package org.jeecg.modules.demo.jg.service.impl;

import org.jeecg.modules.demo.jg.entity.Enterprise;
import org.jeecg.modules.demo.jg.entity.HistoricalWaterFlow;
import org.jeecg.modules.demo.jg.entity.ProvinceMiddle;
import org.jeecg.modules.demo.jg.entity.Region;
import org.jeecg.modules.demo.jg.mapper.WaterMapper;
import org.jeecg.modules.demo.jg.service.WaterService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service
public class WaterServiceImpl implements WaterService {
    @Autowired
    WaterMapper waterMapper;

    @Override
    public List waterInDis() {
        List<Region> regionList = waterMapper.waterInDis();
        int waterInjection = 0;
        int waterDischarged = 0;
        for (int i=0;i<regionList.size();i++) {
            waterInjection += regionList.get(i).getWaterInjection();
            waterDischarged += regionList.get(i).getWaterDischarge();
        }
        List list = new ArrayList();
        list.add(waterInjection);
        list.add(waterDischarged);
        return list;
    }

    @Override
    public List findAllEnterprise() {
        List<Enterprise> list = waterMapper.findAllEnterprise();
        //企业总数
        int countNumber = list.size();
        //废气企业
        int wasteGas = 0;
        //废水企业
        int wasteWater = 0;
        //铅污染企业
        int lead = 0;
        //铬污染企业
        int chromium = 0;
        LinkedList linkedList = new LinkedList();
        linkedList.add(countNumber);
        for (int i=0;i<list.size();i++) {
            int type = list.get(i).getType();
            switch (type) {
                case 1 :
                    wasteGas++;
                    break;
                case 2:
                    wasteWater++;
                    break;
                case 3:
                    lead++;
                    break;
                case 4:
                    chromium++;
                    break;
                default:
                    break;
            }
        }
        linkedList.add(wasteGas);
        linkedList.add(wasteWater);
        linkedList.add(lead);
        linkedList.add(chromium);
        return linkedList;
    }

    @Override
    public List<List> fndTopFive() {
        List<Region> regionList = waterMapper.fndTopFive();
        List<List> list1=new ArrayList<>();
        for (int i=0;i<6;i++) {
            List list = new LinkedList();
            int proviceId = regionList.get(i).getProviceId();
            String provinceName = waterMapper.findProvince(proviceId);
            list.add(regionList.get(i).getPollution());
            list.add(provinceName);
            list.add(regionList.get(i).getName());
            list.add(regionList.get(i).getPollution());
            list.add(regionList.get(i).getRangeArea());
            list1.add(list);
        }
        return list1;
    }

    @Override
    public List<Region> findWaterType() {
        List<Region> list = waterMapper.findWaterType();
        int waterType = 0;
        int drinkingWater = 0;
        int domesticWater = 0;
        int tapWater = 0;
        for (int i=0;i<list.size();i++) {
            waterType = list.get(i).getWaterType();
            switch (waterType) {
                case 1:
                    drinkingWater++;
                    break;
                case 2:
                    domesticWater++;
                    break;
                case 3:
                    tapWater++;
                    break;
                default:
                     break;
            }
        }
        List listResult = new ArrayList();
        listResult.add(drinkingWater);
        listResult.add(domesticWater);
        listResult.add(tapWater);
        listResult.add(100);
        return listResult;
    }

    @Override
    public List<List> findMainDischarge() {
        List<HistoricalWaterFlow> arrList = waterMapper.findMainDischarge();
        List<List> list1 = new ArrayList<>();
        for (int i=0;i<arrList.size();i++) {
            List list = new ArrayList<>();
            list.add(arrList.get(i).getParticularYear());
            list.add(arrList.get(i).getUpperReach());
            list.add(arrList.get(i).getDownStream());
            list.add(arrList.get(i).getAveStream());
            list1.add(list);
        }
        return list1;
    }

    @Override
    public List<List> findMiddleData() {
        List<ProvinceMiddle> list = waterMapper.findMiddleData();
        System.out.println(list);
        List<List> list1 = new ArrayList<>();
        List list01 = new ArrayList();
        List list02 = new ArrayList();
        List list03 = new ArrayList();
        int year = 0;
        int a=0,b=0,c=0;
        for (int i=0;i<list.size();i++) {
            year = list.get(i).getYear();
            if (year == 2018) {
                if(a==0)list01.add(2018);
                int provinceId = list.get(i).getProvinceId();
                int scoreId = list.get(i).getProvinceId();
                String provinceName = waterMapper.findProvince(provinceId);
                int scoreNum = waterMapper.findScoreNum(scoreId);
                list01.add(provinceName);
                list01.add(scoreNum);
                a++;
            } else if (year == 2017) {
                if(b==0)list02.add(2017);
                int provinceId = list.get(i).getProvinceId();
                int scoreId = list.get(i).getProvinceId();
                String provinceName = waterMapper.findProvince(provinceId);
                int scoreNum = waterMapper.findScoreNum(scoreId);
                list02.add(provinceName);
                list02.add(scoreNum);
                b++;
            } else if (year == 2016) {
                if(c==0)list03.add(2016);
                int provinceId = list.get(i).getProvinceId();
                int scoreId = list.get(i).getProvinceId();
                String provinceName = waterMapper.findProvince(provinceId);
                int scoreNum = waterMapper.findScoreNum(scoreId);
                list03.add(provinceName);
                list03.add(scoreNum);
                c++;
            }
        }
        list1.add(list01);
        list1.add(list02);
        list1.add(list03);
        return list1;
    }


}
